Repository navigation
describe() and alter() see secondary indexes and [unique] foreign keys on PostgreSQL - #1751
Open
dimitri-yatsenko wants to merge 3 commits into
Open
dimitri-yatsenko wants to merge 3 commits into
dimitri-yatsenko wants to merge 3 commits into
Conversation
On PostgreSQL, get_indexes_sql returned indexname and indexdef from pg_indexes, while Heading reads index_name, column_name, non_unique and seq_in_index. heading.indexes was therefore always empty: describe() dropped [unique] foreign keys and secondary indexes, and alter() with an unchanged definition raised NotImplementedError on any table with a secondary index. The query now reads pg_index and pg_attribute and returns the same columns as the MySQL query, one row per key column, ordered by index name and position, without the primary-key index. An expression in an index has no pg_attribute row, so it comes back with a NULL column_name, which Heading skips as it does for MySQL's functional key parts. Fixes #1699
dimitri-yatsenko
requested review from
MilagrosMarin,
lum-agilyti and
ttngu207
November 23, 2026 22:58
…ex order An index that mixes columns and expressions was listed by its plain columns alone, so describe() showed a different index (a stricter one when unique) and could hide a plain index on the same columns. The heading now leaves out any index that contains an expression, on both backends. alter() compared index lists in order, but describe() lists indexes by index name and the names differ between backends. The comparison now ignores order.
Add a table that declares index(z) before index(a), so declared order differs from index-name order on both backends. Reword the comment on Ordered: its declared order differs from index-name order only on PostgreSQL.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On PostgreSQL,
heading.indexeswas always empty. The index query returnedindexnameandindexdeffrompg_indexes, but the heading readsindex_name,column_name,non_uniqueandseq_in_index. As a result:describe()dropped the[unique]option of foreign keys and every secondary index.alter()with an unchanged definition raisedNotImplementedError: table.alter cannot alter indexes (yet)on any table with a secondary index.The query now returns the same columns as on MySQL. For the table in the issue,
heading.indexesanddescribe()are now the same on both backends, andalter()with an unchanged definition does nothing.Fixes #1699
Behavior changes
(a, lower(b))was shown bydescribe()asUNIQUE INDEX (a): a stricter constraint than the real one. It could also replace a plainINDEX (a)inheading.indexes, because both map to the key('a',). Now any index that contains an expression is left out ofheading.indexesanddescribe(). This also changes MySQL:describe()no longer shows the plain-column part of a functional index that mixes columns and expressions.alter()compares indexes without regard to order.describe()lists indexes in index-name order. MySQL names an index after its first column, and PostgreSQL names itidx_<table>_<columns>, so the order can differ between backends. For example, with columnsa,a_b,cand the indexesindex(a, c)andindex(a_b), MySQL listsINDEX (a, c)first and PostgreSQL listsINDEX (a_b)first.alter()compared the lists in order. On PostgreSQL it raisedNotImplementedErrorfor that table with an unchanged definition, while MySQL succeeded. On either backend it also raised for any table whose declared index order differed from index-name order, such as one that declaresindex(z)beforeindex(a). It now compares the indexes as sorted lists.Changes
adapters/postgres.py:get_indexes_sqlreadspg_indexandpg_attribute. It returns one row per key column withindex_name,column_name,non_uniqueandseq_in_index, ordered by index name and position, as the MySQL query does.indisprimary).INCLUDEare left out. They are not part of the key.pg_attribute, so it comes back with a NULLcolumn_name, as on MySQL. Loading the heading does not fail.adapters/base.py: the docstring ofget_indexes_sqlnames the columns the heading reads.heading.py: an index with any NULLcolumn_name(an expression) is left out ofheading.indexes. Before, only an index made entirely of expressions was left out.declare.py:alter()compares the index lists of the new definition and ofdescribe()after sorting them.Tests
tests/integration/test_heading_indexes.py, on MySQL and PostgreSQL, with theChildtable from the issue:heading.indexesequals what MySQL reports, in the same order.describe()returns the same text as on MySQL, including-> [unique] Parent,INDEX (a)andUNIQUE INDEX (b, c).describe()produces the sameCREATE TABLEandCREATE INDEXstatements as the definition. A table declared from it has the same indexes and describes the same way.alter(prompt=False)with an unchanged definition runs no statement and leaves the table as it was.test_expression_index_is_skipped: an index on an expression alone, an index whose first part is an expression, and a unique index on(a, lower(b))are all left out.heading.indexesanddescribe()are unchanged, andINDEX (a)stays non-unique. The mixed-index case fails on both backends without theheading.pychange.test_alter_ignores_index_order, with two tables.alter(prompt=False)with an unchanged definition runs no statement on either. Thea,a_b,ctable above fails on PostgreSQL on master. A table that declaresindex(z)beforeindex(a), so that declared order differs from index-name order on both backends, fails on both backends on master.Childtests fail on PostgreSQL on master and pass on MySQL before and after.test_expression_index_is_skippedfails on both backends without theheading.pychange.tests/unit/test_adapters.py: the index query of each adapter returns the four columns the heading reads, and the PostgreSQL query leaves out the primary key. The PostgreSQL cases fail on master.test_alter.py,test_alter_rename.py,test_declare.py,test_fk_index.py,test_foreign_keys.py,test_json.py,test_multi_backend.py) and the unit suite pass locally.Notes for reviewers
WHEREclause) created outside DataJoint are listed like ordinary indexes on PostgreSQL. DataJoint does not declare them.Merge order
alter()on PostgreSQL) also editsalter()indeclare.py.git merge-treeof this branch with that PR reports no conflict: that PR changes other lines ofalter(), and this PR changes only the index comparison line, which that PR leaves as it is. With both merged,test_heading_indexes.pyandtest_alter.pypass on both backends. Either can merge first.heading.indexesanddescribe()on both backends. That includes a mixed index such asunique index(item_id, data.name:char(20)), which was shown asUNIQUE INDEX (item_id)before theheading.pychange.alter()with an unchanged definition raisesNotImplementedErroron such a table on both backends, becausedescribe()cannot show the JSON path.test_describeintests/integration/test_json.pyis skipped for the same reason.adapters/postgres.pyto read the system catalogs. It does not conflict with this PR.